import * as index from '@/api/index.js'
import {
	baseURL
} from '@/common/config.js'
export default {
	data() {
		return {
			disabled: false,
			form: {
				id: '',
				card_image:'',
				name: '',
				mobile: '',
				province: '', //省份
				city: '', //市
				area: '', //区
				con_ids: [],
				con_json: []

			},
			rules: {
				card_image: {
					rules: [{
						required: true,
						errorMessage: '请输入手持身份证照片',
					}]
				},
				name: {
					rules: [{
						required: true,
						errorMessage: '请输入技师姓名',
					}]
				},
				mobile: {
					rules: [{
						required: true,
						errorMessage: '请输入技师手机',
					},
					{
						minLength: 11,
						maxLength: 11,
						errorMessage: '请填写正确的手机号',
					}]
				},
				province: {
					rules: [{
						required: true,
						errorMessage: '请输入技师所在区域',
					}]
				}
			},
			cy: '', //车衣
			gs: '', //改色
			blm: '', //玻璃膜
			checkbox: [],
			status: -1,
			checked:false,
			hobby: [{
				text: '车衣',
				value: 1,
			}, {
				text: '改色',
				value: 2,
			}, {
				text: '玻璃膜',
				value: 3
			}],
		}
	},

	onShow() {

	},
	onLoad(option) {
		// this.getConList()
		this.getMyWorker()
	},
	onReady() {
		// 需要在onReady中设置规则
		this.$refs.form.setRules(this.rules)
	},
	methods: {
		getMyWorker() {
			index.getMyWorker().then(res => {
				if (res.data) {
					if (res.data.check_status == 0||res.data.check_status == 1) {
						this.disabled = true
					}
					this.form.id = res.data.id
					this.form.card_image = res.data.card_image
					this.form.name = res.data.name
					this.form.mobile = res.data.mobile
					this.form.province = res.data.province
					this.form.city = res.data.city
					this.form.area = res.data.area
					this.form.con_ids = res.data.con_ids.split(',')
					// this.checkbox = 
					this.checkbox = []
					this.form.con_ids.forEach(item => {
						this.checkbox.push(parseInt(item))
					})
					res.data.con_array.forEach(item => {
						if (item.id == 1) {
							this.cy = item.age
						}
						if (item.id == 2) {
							this.gs = item.age
						}
						if (item.id == 3) {
							this.blm = item.age
						}
					})
					this.status = res.data.check_status
					// res.data.con_ids.split(',')
				}
			})
		},
		submit() {
			
				
			if(this.checked){
				this.$refs.form.validate().then(res => {
					console.log('表单数据信息：', res);
					let arr = []
					this.form.con_ids = this.checkbox.join()
					if (this.cy) {
						arr.push({
							id: 1,
							name: '车衣施工',
							age: this.cy
						})
					}
					if (this.gs) {
						arr.push({
							id: 2,
							name: '改色施工',
							age: this.gs
						})
					}
					if (this.blm) {
						arr.push({
							id: 2,
							name: '玻璃膜施工',
							age: this.blm
						})
					}
					this.form.con_json = JSON.stringify(arr)
					let that = this
					uni.showModal({
						content: '是否提交技师认证',
						success: function (res) {
							if (res.confirm) {
								index.editWorker(that.form).then(res => {
									that.$tools.toast(res.msg)
									that.getMyWorker()
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				
					
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			}
			else{
							this.$tools.toast('请先阅读并同意《服务规范》')
						}
		
		},
		pickerChange(e) {
			this.form.province = e.detail.value[0]
			this.form.city = e.detail.value[1]
			this.form.area = e.detail.value[2]
		},
		radioClick() {
			this.checked = !this.checked
		},
		goWebview(item) {
				this.$tools.navigate('/pages/user/webview/index?name=' + item)
			},
		dataChange(e) {
			console.log();
			if (e.detail.value) {
				if (e.detail.value.indexOf(1) < 0) {
					this.cy = ''
				}
				if (e.detail.value.indexOf(2) < 0) {
					this.gs = ''
				}
				if (e.detail.value.indexOf(3) < 0) {
					this.blm = ''
				}
			} else {
				this.cy = this.gs = this.blm = ''
			}

		},
		uploadImage(){
			let that = this
			uni.chooseMedia({
				count: 1,
				mediaType: ['image'],
				sourceType: ['album', 'camera'],
				maxDuration: 30,
				camera: 'back',
				success(res) {
					console.log(res,'res');
					uni.uploadFile({
						url: baseURL + 'common/upload', //仅为示例，非真实的接口地址
						filePath: res.tempFiles[0].tempFilePath,
						name: 'file',
						formData: {
							'user': 'test',
							'Content-type': 'application/json',
							'token': uni.getStorageSync('token'),
						},
						success(images) {
							console.log(images)
							that.form.card_image  = JSON.parse(images.data).data.fullurl
							//do something
						}
					})
			
				}
			})
		}
		// getConList(){
		// 	index.getConList().then(res=>{
		// 		let arr = []
		// 		res.data.forEach(item=>{
		// 			arr.push({
		// 				text:item.name,
		// 				value:item.id
		// 			})
		// 		})
		// 		this.hobby = arr
		// 	})
		// }
	}
}